package com.yanqu.road.server.logic.activity.condition.normal;

import com.yanqu.road.entity.activity.ActivityConditionInfo;
import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.enums.eGamePlayerEventType;
import com.yanqu.road.entity.enums.ePlayerAttrType;
import com.yanqu.road.entity.enums.eWelfareActivityType;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.player.AttributeModule;
import com.yanqu.road.server.gameplayer.module.player.EarnSpeedModule;
import com.yanqu.road.server.gameplayer.module.task.TaskModule;
import com.yanqu.road.server.logic.activity.BaseActivityData;
import com.yanqu.road.server.logic.activity.condition.BaseActivityCondition;

import java.math.BigInteger;

/**
 * 赚速基金
 */
public class EarnSpeedFundCondition extends BaseActivityCondition {

	public EarnSpeedFundCondition(ActivityInfo activityInfo, ActivityConditionInfo activityConditionInfo, BaseActivityData baseActivityData) {
		super(activityInfo, activityConditionInfo, baseActivityData);
	}

	@Override
	public void addTrigger(GamePlayer player) {
		player.getModule(TaskModule.class).getGameEvent().addListener(eGamePlayerEventType.EarnSpeedFund.getValue(), handle);
	}

	@Override
	public void removeTrigger(GamePlayer player) {
		player.getModule(TaskModule.class).getGameEvent().removeListener(eGamePlayerEventType.EarnSpeedFund.getValue(), handle);
	}

	public boolean isCompleted(GamePlayer player) {
		ePlayerAttrType attrType;
		if (activityInfo.getChildType() == eWelfareActivityType.EarnMoneyFund.getValue()) {
			attrType = ePlayerAttrType.EarnSpeedFund;
		} else if (activityInfo.getChildType() == eWelfareActivityType.EarnMoneyPlusFund.getValue()) {
			attrType = ePlayerAttrType.EarnSpeedPlusFound;
		} else {
			return false;
		}
		if (activityConditionInfo.getParamList().get(0).compareTo(BigInteger.ONE) == 0) {//第一位是是否要購買過
			//是否購買了
			if (player.getModule(AttributeModule.class).getAttribute(attrType) == 1) {
				return getValue().compareTo(activityConditionInfo.getParamList().get(3)) >= 0;
			}
		} else if (activityConditionInfo.getParamList().get(0).compareTo(BigInteger.ZERO) == 0) {
			return getValue().compareTo(activityConditionInfo.getParamList().get(3)) >= 0;
		}
		return false;
	}

	@Override
	public void initDefaultValue() {
		BigInteger earnSpeed = player.getModule(EarnSpeedModule.class).getEarnSpeed();
		if (earnSpeed.compareTo(getValue()) > 0) {
			setValue(earnSpeed);
		}
	}

	@Override
	protected void player_BoardLevelHandle(Object object) {
		BigInteger args = (BigInteger) object;
		if(args.compareTo(getValue()) > 0) {
			setValue(args);
		}
	}

}
